﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace L07
{
    class Program
    {
        static void Main(string[] args)
        {
            Cock cock = new Cock("花花");
            Host host = new Host("祖逖");
            host.Foster(cock);
            Dog dog = new Dog("阿黄");
            dog.Owner = host;
            Cat cat = new Cat("阿咪");
            cat.Owner = host;
            Sun.Rise();

            Console.ReadKey();
        }

        public static class Sun
        {
            public static event EventHandler OnRise;

            public static void Rise()
            {
                Console.WriteLine("太阳从东边升起...");
                if (OnRise != null)
                {
                    OnRise(null, null);
                }
            }
        }

        public class Cock//公鸡
        {
            private string name;

            public Cock(string name)
            {
                this.name = name;
                Sun.OnRise += new EventHandler(Sun_OnRise);
            }

            private void Sun_OnRise(object sender, EventArgs e)
            {
                Console.WriteLine("公鸡{0}：", name);
                Console.WriteLine("雄鸡一唱天下白");
                this.Sing();
            }

            public event EventHandler OnSing;

            public void Sing()
            {
                Console.WriteLine("喔喔喔...");
                if (OnSing != null)
                {
                    OnSing(this, null);
                }
            }
        }

        public class Host//主人
        {
            private string name;

            public Host(string name)
            {
                this.name = name;
            }

            public event EventHandler OnGetup;

            public void Getup()
            {
                Console.WriteLine("日出而作...");
                if (OnGetup != null)
                {
                    OnGetup(this, null);
                }
            }

            public void Foster(Cock c1)
            {
                c1.OnSing += new EventHandler(c1_OnSing);
            }

            private void c1_OnSing(object sender, EventArgs e)
            {
                Console.WriteLine("主人{0}：", name);
                Console.WriteLine("闻鸡起舞！");
                this.Getup();
            }
        }

        public class Dog//狗
        {
            private string name;
            private Host owner;

            internal Host Owner {
                get { return owner; }
                set { owner = value; owner.OnGetup += new EventHandler(owner_OnGetup); }
                 }

            public Dog(string name)
            {
                this.name = name;                
            }

            private void owner_OnGetup(object sender, EventArgs e)
            {
                Console.WriteLine("狗狗{0}：", name);
                this.Sing();
            }

            public event EventHandler OnSing;

            public void Sing()
            {
                Console.WriteLine("汪汪...");
                if (OnSing != null)
                {
                    OnSing(this, null);
                }
            }
        }

        public class Cat//猫
        {
            private string name;
            private Host owner;

            internal Host Owner {
                get { return owner; }
                set { owner = value; owner.OnGetup += new EventHandler(owner_OnGetup); }
            }

            public Cat (string name)
            {
                this.name = name;
            }

            private void owner_OnGetup(object sender, EventArgs e)
            {
                Console.WriteLine("猫咪{0}：", name);
                this.Sing();
            }

            public event EventHandler OnSing;

            public void Sing()
            {
                Console.WriteLine("喵喵...");
                if (OnSing != null)
                {
                    OnSing(this, null);
                }
            }
        }

    }
}
